﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;

namespace GenericHostSample
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            Logger logger = null;

            var hostBuilder = new HostBuilder()
                .UseServiceProviderFactory(new AutofacContainerFactory())
                .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.AddEnvironmentVariables();
                    config.AddJsonFile("appsettings.json", optional: true);
                    config.AddCommandLine(args);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<MyServiceA>();
                    //services.AddHostedService<MyServiceB>();
                })
                .ConfigureLogging((context, builder) =>
                {
                    logger = new LoggerConfiguration()
                        .ReadFrom.Configuration(context.Configuration)
                        .CreateLogger();
                    builder.AddSerilog(logger);
                });

            await hostBuilder.RunConsoleAsync();
            //await hostBuilder.Build().RunAsync();
        }
    }
}